<!doctype html>
<title>Check you don't crash when your previous drag target frame is deleted</title>
<script src="../../resources/testharness.js"></script>
<script src="../../resources/testharnessreport.js"></script>
<p>Check you don't crash when your previous drag target frame is deleted</p>
<div><span id="dragme">hello</span></div>
<iframe id="target"></iframe>
<div id="log"></div>
<script>
var framex;
var framey;
var target = document.getElementById("target");
target.contentDocument.designMode = 'on';
var t = async_test('Drag-And-Drop to dead frame');

window.addEventListener('load', t.step_func(() => {
    if (!window.eventSender)
        throw new Error('This test requires eventSender.');

    framex = target.offsetLeft + 10;
    framey = target.offsetTop + target.offsetHeight / 2;

    // Drag 'hello'
    var dragMe = document.getElementById("dragme");
    window.getSelection().selectAllChildren(dragMe);
    x = dragMe.offsetLeft + 10;
    y = dragMe.offsetTop + dragMe.offsetHeight / 2;
    eventSender.mouseMoveTo(x, y);
    eventSender.mouseDown();

    eventSender.leapForward(500);
    eventSender.mouseMoveTo(framex, framey);
    setTimeout(dropIt1, 0);
}));

function dropIt1() {
    // Drop it off to the frame
    eventSender.mouseUp();
    assert_equals(target.contentDocument.body.textContent, 'hello',
                  'target should have dropped content');
    setTimeout(deleteFrameAndDrop, 0);
}

function deleteFrameAndDrop() {

    // Delete the frame where we just dragged somet stuff
    target.remove();

    // Do drag drop again
    var dragMe = document.getElementById("dragme");
    x = dragMe.offsetLeft + 10;
    y = dragMe.offsetTop + dragMe.offsetHeight / 2;
    eventSender.mouseMoveTo(x, y);
    eventSender.mouseDown();
    eventSender.leapForward(500);
    eventSender.mouseMoveTo(framex, framey);
    setTimeout(dropIt2, 0);
}

function dropIt2() {
    // Drop it off to somewhere
    eventSender.mouseUp();
    // Since we don't crash, we are succeeded!
    t.done();
}
</script>
